<?php

namespace Endor\EndorBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Endor\EndorBundle\Entity\Bodegas;
use Endor\EndorBundle\Entity\Articulos;
use Endor\EndorBundle\Entity\Salidas;
use Endor\EndorBundle\Entity\Entradas;
use Endor\EndorBundle\Model\InventarioModel;
use Symfony\Component\HttpFoundation\Response;

class EnsambladoController extends Controller {

    public function indexAction() {
        $em = $this->getDoctrine()->getEntityManager();

        $entities = $em->getRepository('EndorBundle:Articulos')->findBy(array(
            'habilitado' => '1',
            'Kit' => NULL
                ));

        return $this->render('EndorBundle:Ensamblado:index.html.twig', array(
                    'entities' => $entities
                ));
    }

    public function newAction() {
        $request = $this->getRequest();

        $id = $request->get("id");

        $em = $this->getDoctrine()->getEntityManager();
        $entity = $em->getRepository("EndorBundle:Articulos")->find($id);

        $entities = $em->getRepository("EndorBundle:Articulos")->findBy(array(
            'Kit' => $id));
        
        $bodegas = $em->getRepository("EndorBundle:Bodegas")->findAll();

        return $this->render('EndorBundle:Ensamblado:new.html.twig', array(
                    'entity'  => $entity,
                    'piezas'  => $entities,
                    'bodegas' => $bodegas
                ));
    }

    public function createAction() {
        $entity = new Articulos();
        $piezas = array();
        $request = $this->getRequest();
        
        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository("EndorBundle:Articulos")->find($request->get("id"));

        $cantidad = $request->get("cantidad");
        $piezas = $request->get("piezas");

        foreach ($piezas as $pieza) {
            $salida = new Salidas();

            $articulo = $em->getRepository("EndorBundle:Articulos")->find($pieza["idArticulo"]);
            $bodega = $em->getRepository("EndorBundle:Bodegas")->find($pieza["idBodega"]);
            //$cliente = $em->getRepository("EndorBundle:Clientes")->find(0);

            $salida->setArticulo($articulo);
            $salida->setBodega($bodega);
            $salida->setCantidad($cantidad);
            $salida->setCliente(NULL);
            $salida->setFecha(date_create());
            $salida->setIsTipoUnidad(TRUE);

            $em->persist($salida);
            $em->flush();
        }

        $entrada = new Entradas();

        $entrada->setArticulo($entity);
        $entrada->setBodega($em->getRepository("EndorBundle:Bodegas")->find($request->get("bodega")));
        $entrada->setCantidad($cantidad);
        $entrada->setFechaEntrada(date_create());
        $entrada->setFechaVencimiento(NULL);
        $entrada->setProveedor(NULL);

        $em->persist($entrada);
        $em->flush();
        
        return $this->redirect($this->generateUrl('ensamblado'));
        
    }
    
    public function getStockAction(){
        $request = $this->getRequest();
        
        $idBodega = $request->get('idBodega');
        $idArticulo = $request->get('idArticulo');
        
        $em = $this->getDoctrine()->getEntityManager();
        
        $model = new InventarioModel($this->getDoctrine()->getConnection());
        $emResult = $model->GetByBodegaArticulo($idBodega, $idArticulo);
        
        if(!empty ($emResult))            
            $result = $emResult[0]['Cantidad'];
        else
            $result = '0';
        
         return new Response($result);
    }

}

?>
